﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;

using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using System.Data;

namespace SmallERP.Bll.BaseInfo
{
    public  class Paperface:IDBAccess
    {
        /// <summary>
        /// 获得纸面列表
        /// </summary>
        /// <returns></returns>
        public List<PaperFace_Info> GetPaperFaceList()
        {
            List<PaperFace_Info> list = new List<PaperFace_Info>();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    List<PaperFace> listPaperFace = entities.PaperFace.ToList();
            //    foreach (var item in listPaperFace)
            //    {
            //        PaperFace_Info paperFace = new PaperFace_Info();
            //        paperFace.PaperFaceId = item.PaperFaceId;
            //        paperFace.PaperFaceName = item.PaperFaceName;
            //        list.Add(paperFace);
            //    }
            //}
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperFace");
            for (int i=0;i<dt.Rows.Count;i++)
            {
                DataRow row=dt.Rows[i];
                PaperFace_Info paperFace = new PaperFace_Info();
                paperFace.PaperFaceId = Convert.ToInt32(row["PaperFaceId"]);
                paperFace.PaperFaceName =(row["PaperFaceName"]).ToString();
                list.Add(paperFace);
 
            }
            return list;
        }

        /// <summary>
        /// 获得纸面
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PaperFace_Info GetPaperFace(string name)
        {
          
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    PaperFace paperface = entities.PaperFace.FirstOrDefault(p => p.PaperFaceName == name);
            //    if (paperface != null)
            //    {
            //        paperFaceInfo.PaperFaceId = paperface.PaperFaceId;
            //        paperFaceInfo.PaperFaceName = paperface.PaperFaceName;
            //    }
            //    else
            //    {
            //        paperFaceInfo = null;
            //    }
            //}
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperFace where PaperFaceName='"+name+"'");
            
                DataRow row = dt.Rows[0];
                PaperFace_Info paperFaceInfo = new PaperFace_Info();
                paperFaceInfo.PaperFaceId = Convert.ToInt32(row["PaperFaceId"]);
                paperFaceInfo.PaperFaceName = (row["PaperFaceName"]).ToString();
                
           
            return paperFaceInfo;
        }

        #region IDBAccess 成员

        /// <summary>
        /// 添加纸面
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Add(object o)
        {
            int ret = 0;
            //PaperFace_Info paperFaceInfo = (PaperFace_Info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperFace pInDataBase = entities.PaperFace.FirstOrDefault(p=>p.PaperFaceName ==paperFaceInfo.PaperFaceName);
            //        if (pInDataBase != null)
            //        {
            //            return -2;
            //        }
            //        PaperFace paperFace = new PaperFace();
            //        paperFace.PaperFaceName = paperFaceInfo.PaperFaceName;
            //        entities.AddToPaperFace(paperFace);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            PaperFace_Info paperFaceInfo = (PaperFace_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperFace where PaperFaceId=" + paperFaceInfo.PaperFaceId + "");
            if (dt.Rows.Count == 0)
            {
                try
                {
                    string insertSql = "insert into PaperFace (PaperFaceName) values('" + paperFaceInfo.PaperFaceName + "')";
                    ret=DAL.AccessHelper.ExecuteNonQuery(insertSql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;

        }
        /// <summary>
        /// 更新纸面
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Update(object o)
        {
            int ret = 0;
            //PaperFace_Info paperFaceInfo = (PaperFace_Info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperFace pInDataBase = entities.PaperFace.FirstOrDefault(p=>p.PaperFaceName ==paperFaceInfo.PaperFaceName);

            //        PaperFace paperFace = entities.PaperFace.FirstOrDefault(p=>p.PaperFaceId ==paperFaceInfo.PaperFaceId);
            //        if (pInDataBase != null && pInDataBase.PaperFaceId != paperFace.PaperFaceId)
            //        {
            //            return -2;
            //        }
            //        paperFace.PaperFaceName = paperFaceInfo.PaperFaceName;
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            PaperFace_Info paperFaceInfo = (PaperFace_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperFace where PaperFaceId=" + paperFaceInfo.PaperFaceId + "");
            if (dt.Rows.Count != 0)
            {
                try
                {
                    string insertSql = "update PaperFace  set PaperFaceName='" + paperFaceInfo.PaperFaceName + "' where PaperFaceId=" + paperFaceInfo.PaperFaceId;
                    ret =DAL.AccessHelper.ExecuteNonQuery(insertSql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;


          
        }

        /// <summary>
        /// 删除纸面
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Delete(object o)
        {
            int ret = 0;
            PaperFace_Info paperFaceInfo = (PaperFace_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperFace where PaperFaceId=" + paperFaceInfo.PaperFaceId + "");
            if (dt.Rows.Count != 0)
            {
                try
                {
                    string insertSql = "delete from PaperFace  where PaperFaceId=" + paperFaceInfo.PaperFaceId ;
                    DAL.AccessHelper.excuteSql(insertSql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;
        }

        public int LinkGridControl(object o)
        {
            int ret = 0;
            try
            {
                ((GridControl)o).DataSource = GetPaperFaceList();
                ((GridControl)o).RefreshDataSource();
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        #endregion
    }
}
